package com.web.base.open.provider;

import com.blue.core.tree.TreeBuilder;
import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.open.cache.RoleUrlCache;
import com.web.base.open.exception.OpenException;
import com.web.base.open.facade.RoleFacade;
import com.web.base.open.message.RoleRequest;
import com.web.base.open.message.RoleResponse;
import com.web.base.open.message.RoleUrlRequest;
import com.web.base.open.message.UrlResponse;
import com.web.base.open.model.Role;
import com.web.base.open.model.RoleUrl;
import com.web.base.open.model.Url;
import com.web.base.open.service.RoleService;
import com.web.base.open.service.RoleUrlService;
import com.web.base.open.util.RoleConvertor;
import com.web.base.open.util.RoleUrlConvertor;
import com.web.base.open.util.UrlConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component("openRoleProvider")
public class RoleProvider implements RoleFacade
{
	@Autowired
	private RoleService roleService;
	@Autowired
	private RoleUrlService roleUrlService;
	@Autowired
	private RoleUrlCache roleUrlCache;

	public RoleProvider()
	{
	}

	@Override
	public RoleResponse add(RoleRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		Role role = RoleConvertor.toRole(request);
		role = roleService.save(role);
		RoleResponse response = RoleConvertor.toResponse(role);
		return response;
	}

	@Override
	public RoleResponse update(RoleRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		Role role = RoleConvertor.toRole(request);
		role = roleService.save(role);
		RoleResponse response = RoleConvertor.toResponse(role);
		return response;
	}

	@Override
	public RoleResponse get(Integer id)
	{
		if (id == null)
			throw new OpenException("角色ID不能为空");
		
		Role role = roleService.get(id);
		RoleResponse response = RoleConvertor.toResponse(role);
		return response;
	}

	@Override
	public Page<RoleResponse> listPage(RoleRequest request, int start, int size)
	{
		Role role = RoleConvertor.toRole(request);
		Page<Role> page = new Page<>(size);
		page.setCurPage(start);
		page = roleService.list(role, page);
		Page<RoleResponse> responsePage = RoleConvertor.toResponse(page);
		return responsePage;
	}

	@Override
	public Map<String, List<UrlResponse>> listSelect(Integer id)
	{
		if (id == null)
			throw new OpenException("角色ID不能为空");
			
		List<Url> list = roleUrlService.listSelect(id);
		List<UrlResponse> responseList = UrlConvertor.toResponse(list);
		Map<String, List<UrlResponse>> map = TreeBuilder.buildMenu(responseList);
		return map;
	}

	@Override
	public void saveRoleUrl(RoleUrlRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		RoleUrl roleUrl = RoleUrlConvertor.toRoleUrl(request);
		roleUrlService.save(roleUrl);
		roleUrlCache.delete(request.getRoleId());
	}

	@Override
	public void refresh()
	{
		roleUrlCache.delete(null);
	}

}
